package com.sean.framework.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

/**
 * 图像工具
 * @author Sean
 */
public class ImageUtil {

	public static final String TAG = "ImageUtil";
	
	public static Bitmap getBitmapByUrl(String link) throws IOException {
		
		Bitmap bitmap = null;
		if(link.startsWith("http://")) {
			URL url = new URL(link);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setConnectTimeout(5000);
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream in = conn.getInputStream();
				bitmap = BitmapFactory.decodeStream(in);
				in.close();
			}
			conn.disconnect();
			Log.i(TAG, "getBitmapByUrl >> Has get the bitmap from the url : " + link);
		} else {
			throw new IOException("The String of URL must start witch 'http://'");
		}
		return bitmap;
	}
}
